(function() {
  Event.addBehavior({
    ".hover_info .deal_tags li:click": function(e){
      e.stopPropagation();
    },

    '.hoverable .image:mouseenter': function(e){
      if($('html').hasClass('ie8'))
        $('.hover_info', this).show();
      else
        $('.hover_info', this).stop().fadeTo(300, 1);
      if($(this).parents('.three_up').length > 0 && $('.title', this).height() > 50){
        $('.deal_tags', this).height(32);
      }
    },

    '.hoverable .image:mouseleave': function(e){
      if($('html').hasClass('ie8'))
        $('.hover_info', this).hide();
      else
        $('.hover_info', this).stop().fadeTo(300, 0);
    }
  });


  // This hack is only needed to re-render problematic images
  // It waits for all assets on the page to fully load and then
  // resets the src attr to itself on deal tile images
  var fixPartiallyLoadedImages = function() {
    $('.hoverable img').each(function() {
      var $self = $(this);
      $self.attr('src', $self.attr('src'));
    });
  };

  if ($(document.body).hasClass('getaways') || $(document.body).hasClass('goods')) {
    $(window).load(function() {
      setTimeout(fixPartiallyLoadedImages, 0);
    });
  }

  $(function() {
    if ($("#places_filter").length > 0 && Groupon.PlacesDropdown) {
      Groupon.onCurrentUserReady(function() {
        Groupon.PlacesDropdown.init(Groupon.currentUser.places, Cookie.get('search_loc'));
      });
    }

    // Show the Filters
    $('#filters').show();
    if(Groupon.Dale) {
      Groupon.onCurrentUserReady(function() {
        if (App.Views.Now.DealsWidget) {
          new App.Views.Now.DealsWidget({
            template: Groupon.MustacheTemplates['groupon/deals/all_deals/_now_deal_group'],
            limit: Groupon.Attributes.now_widget_deals,
            precision: Groupon.Attributes.now_api_cache_precision,
            header: Groupon.Attributes.now_widget_header
          });
        }
      });
    }
  });

  $(document).bind('dealsRendered', function() {
    $('.counter').each(function() {
      var deal_id = $(this).data("deal-id");
      var deadline = $(this).data("deadline-at");
      var container = "#" + $(this).data("countdown-container");

      var countdown = new Countdown({
        targetTime: deadline,
        numOfIntervalsToShow: 4,
        domContainer: container,
        outputFormat: {
          days: "{days} day(s) ",
          hours: "{hours}:",
          minutes: "{minutes}:",
          seconds: "{seconds} "
        },
        pad: true,
        numOfIntervalsToPad: 2,
        expiringSoonThreshold: 3600,
        showIfZero: {
          days: false,
          hours: true,
          minutes: true,
          seconds: true
        }
      }).start();
    })
  });

  $(document).bind('dealsRendered', function() {
    $(".groupon_countdown").countdown();
  });

  Groupon.initCarousel = function(data) {
    if(Groupon.onAllDealsPage)
      $(document).bind('dealsRendered', function() {
        (new Groupon.Views.DealCarousel(data)).render();
      });
  };

  if(location.search.toQueryParams().dl)
    $(function() {
      if(Groupon.Personalization && Groupon.Personalization.data) {
        Groupon.initCarousel(Groupon.Personalization.data);
      }

      else if (Groupon.Personalization) {
        $(document).bind('personalizeData', function(data) {
          Groupon.initCarousel(Groupon.Personalization.data);
        });
      }
    });

    $(document).ready(function() {
      if (Groupon.onCurrentUserReady) {
        Groupon.onCurrentUserReady(function(){
          if (Cookie.get('subscriber_email') || Groupon.Application.isLoggedIn())
            $('#division_description').hide();
        });
      }
    });

})();

Groupon.onCurrentUserReady(function(){
  if(!(Groupon.Personalization && Groupon.Personalization.AllDeals) || !Groupon.Personalization.AllDeals.Discount) {
    return;
  }
  (function() {
    var FigureHover;

    FigureHover = (function() {

      function FigureHover(container) {
        this.container = $(container);
        this.section = $(this.container.find('figcaption'));
        this.distance = -(this.section.height() + parseInt(this.section.css('padding-bottom')) * 2) + "px";
        this.initialize();
      }

      FigureHover.prototype.initialize = function() {
        var _this = this;
        this.section.css('bottom', this.distance).show();
        if (this.section.children().length > 0) {
          this.container.mouseenter(function() {
            return _this.show(_this.section);
          });
          return this.container.mouseleave(function() {
            return _this.hide(_this.section);
          });
        }
      };

      FigureHover.prototype.show = function(section) {
        section.stop(true, false);
        return section.animate({
          'bottom': '0px'
        }, 150);
      };

      FigureHover.prototype.hide = function(section) {
        return section.animate({
          'bottom': this.distance
        }, 150);
      };

      return FigureHover;

    })();

    $.fn.figureHover = function() {
      return this.each(function() {
        return new FigureHover(this);
      });
    };

  })();
  $('#deal_container.all_deals_discounted_group .deal').figureHover();
});

